package com.link.base.base.menupageurls.service;

import javax.annotation.Resource;

import com.link.base.base.common.model.Menu;
import com.link.base.base.common.service.MenuService;
import com.link.base.base.menupageurls.utils.RunAddPageUrl;
import com.link.base.base.monitor.MonitorObjectChange;
import com.link.base.base.pageurls.model.PageUrls;
import com.link.base.base.pageurls.service.PageUrlsService;
import com.link.base.core.basic.util.ThreadPoolExecutorSingleton;
import com.link.core.basic.interceptor.SecurityInterceptor;
import com.link.core.util.StringUtils;
import org.springframework.stereotype.Service;
import com.link.base.base.menupageurls.dao.mybatis.mapper.MenuPageUrlsMapper;
import com.link.base.base.menupageurls.model.MenuPageUrls;
import com.link.core.basic.dao.mapper.BasicMapper;
import com.link.core.basic.service.BasicServiceImpl;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ThreadPoolExecutor;

/**
 * 菜单下页面路径
 *
 * @author 秦学刚
 * @date 2020-07-03 16:14:08
 */
@Service
public class MenuPageUrlsServiceImpl extends BasicServiceImpl<MenuPageUrls> implements MenuPageUrlsService {

    @Resource
    private MenuPageUrlsMapper    menuPageUrlsMapper;
    @Resource
    private PageUrlsService pageUrlsService;
    @Resource
    private MonitorObjectChange monitorObjectChange;
    @Resource
    private MenuService menuService;


    @Override
    public BasicMapper<MenuPageUrls> getBasicMapper() {
        return menuPageUrlsMapper;
    }

    @Override
    public List<String> getUrlsByDuty(MenuPageUrls menuPageUrls) throws Exception {
        // step1 查找到职责下面的所有的菜单
        // step2 根据步骤一查找到的菜单，查找到安全性菜单以及 url信息
        // step3 根据安全性菜单以及url信息进行筛选出最后的url信息
        // step4 根据查找出来的url进行剔重处理
        return menuPageUrlsMapper.getUrlsByDuty(menuPageUrls);
    }

    @Override
    public void addPageUrlAll(Menu menu) throws Exception {
        List<String> menuUrl = new ArrayList<>();
        if (!menuUrl.contains(menu.getMenuUrl())) {
            PageUrls pageUrls = new PageUrls();
            pageUrls.setPagePath(menu.getMenuUrl());
            if ("CRM".equalsIgnoreCase(menu.getPlatformType()) || "DMS".equalsIgnoreCase(menu.getPlatformType())) {
                pageUrls.setSystem("PC");
            } else {
                pageUrls.setSystem(menu.getPlatformType());
            }
            pageUrls.setAttr1(menu.getId().toString());
            List<PageUrls> pageUrlsTemp = pageUrlsService.queryByPagePath(pageUrls);
            if (pageUrlsTemp == null || pageUrlsTemp.size() <= 0) {
                return;
            }
            List<MenuPageUrls> menuPageUrls = new ArrayList<>();
            for (PageUrls pageUrls1 : pageUrlsTemp) {
                MenuPageUrls menuPageUrls1 = new MenuPageUrls();
                menuPageUrls1.setPageUrlId(pageUrls1.getId());
                menuPageUrls1.setMenuId(menu.getId());
                menuPageUrls1.setEffective(pageUrls1.getEffective());
                String urlType = pageUrls1.getUrlType();
                if (StringUtils.isBlank(urlType)) {
                    urlType = "Other";
                }
                menuPageUrls1.setUrlType(urlType);
                menuPageUrls1.setUrl(pageUrls1.getUrl());
                menuPageUrls1.setPagePath(pageUrls1.getPagePath());
                menuPageUrls1.setPushPage(pageUrls1.getPushPage());
                menuPageUrls1.setRemarks(pageUrls1.getRemarks());
                menuPageUrls1.setSystem(pageUrls1.getSystem());
                menuPageUrls.add(menuPageUrls1);
            }
            this.batchInsert(menuPageUrls);
            String pagePath = pageUrlsTemp.get(0).getPushPage();
            if (StringUtils.isBlank(pagePath)) {
                return;
            }
            menuUrl.add(pagePath);
            // 递归调用
//            menu.setMenuUrl(pagePath);
//            this.addPageUrlAll(menu);
        }
    }

    @Override
    public void addFirstMenuPageUrlAll() throws Exception {
        ThreadPoolExecutor executor = ThreadPoolExecutorSingleton.getInstance();
        executor.execute(new RunAddPageUrl(SecurityInterceptor.token.get(),
                SecurityInterceptor.tempUser.get(),SecurityInterceptor.tempEnterpirse.get()));
    }

    @Override
    public void beforUpsert(MenuPageUrls menuPageUrls) throws Exception {
        // 这里需要监控菜单下URL的新增/修改/批量编辑,当变化时需要触发职责url变化
        monitorObjectChange.dutyChange(menuPageUrls,"CRM_DMS");
    }

    @Override
    public void beforDelete(MenuPageUrls menuPageUrls) throws Exception {
        // 这里需要监控菜单下URL的删除,当变化时需要触发职责url变化
        monitorObjectChange.dutyChange(menuPageUrls,"CRM_DMS");
    }
}
